﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Services.Client;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NCT.MRS.PresentationLayer.MrsWebService;

namespace NCT.MRS.PresentationLayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AppContext context = new AppContext(new Uri("http://localhost:1242/MrsDataService.svc/"));

            AccessSection acs = context.AccessSections.Expand("AccessTypes").Where(P => P.Id.Equals(3)).FirstOrDefault();

            acs.Name = "MEDTECH";
            
            List<AccessType> acccc = new List<AccessType>()
            {
                new AccessType {Name="Add New MedTech", Code="adda_medtech"}
            };

            
            try
            {
                context.UpdateRelatedEntities(acs, () => acs.AccessTypes, acccc);
                DataServiceResponse resp = context.SaveChanges();
            }
            catch (Exception ex)
            {

            }

            //IQueryable<AccessSection> acss = context.Execute<AccessSection>(new Uri("GetAllAccessTypesBySection", UriKind.Relative)).AsQueryable();

            foreach (AccessSection acsa in context.AccessSections.Expand("AccessTypes").ToList())
            {

                TreeNode node = new TreeNode();

                node.Text = acsa.Name;

                foreach (AccessType at in acsa.AccessTypes)
                {
                    TreeNode nodd = new TreeNode();
                    nodd.Text = at.Name;
                    nodd.Tag = at;
                    node.Nodes.Add(nodd);
                }

                treeView1.Nodes.Add(node);
            }
        }
    }
}
